import sqlalchemy as sa
from sqlalchemy import orm

from formpaginate.model import meta

def init_model(engine):
    """Call me before using any of the tables or classes in the model."""

    # Reflected tables must be defined and mapped here.
    #
    #global t_reflected
    #t_reflected = sa.Table("Reflected", meta.metadata,
    #    autoload=True, autoload_with=engine)
    #orm.mapper(Reflected, t_reflected)

    sm = orm.sessionmaker(autoflush=True, transactional=True, bind=engine)

    meta.engine = engine
    meta.Session = orm.scoped_session(sm)


# Non-reflected tables may be defined and mapped at module level.
#
#t_foo = sa.Table("Foo", meta.metadata,
#    sa.Column("id", sa.types.Integer, primary_key=True),
#    sa.Column("bar", sa.types.String(255), nullable=False),
#    )
#
#class Foo(object):
#    pass
#
#orm.mapper(Foo, t_foo)


# Classes for reflected tables may be defined here, but the table and
# mapping itself must be done in the init_model function.
#
#table_reflected = None    # Updated by init_model().
#
#class Reflected(object):
#    pass
